package com.wkhyapp.lm.view;

import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.wkhyapp.lm.MainActivity;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.CheckInPresenter;
import com.wkhyapp.lm.contract.CheckInView;
import com.wkhyapp.lm.http.net.AddGoodsRequest;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.weigit.dialog.CategoryPickerDialg;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CheckInActivity extends SuperActivity<CheckInPresenter> implements CheckInView {

    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.submit_but)
    Button submit_but;
    @BindView(R.id.wxAccount_et)
    EditText wxAccount;
    @BindView(R.id.phone_et)
    EditText phone;
    @BindView(R.id.type_tv)
    TextView type_tv;
    CategoryPickerDialg pickerDialg;
    private List<Category> categories;
    private int categoryId;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

        mPresenter.getCategory();

    }
    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
            }
        });

        submit_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if("".equals(wxAccount.getText().toString())){
                    TToast("请输入微信号");
                    return;
                }
                if("".equals(phone.getText().toString())){
                    TToast("请输入手机号");
                    return;
                }


                showLoad("申请提交中");
                mPresenter.submit(phone.getText().toString(),wxAccount.getText().toString(),categoryId);
            }
        });

        type_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPickerView();
            }
        });
    }


    private void showPickerView() {
//      要展示的数据
        final List<String> listData = getData();
//      监听选中
        OptionsPickerView pvOptions = new OptionsPickerBuilder(CheckInActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                type_tv.setText(listData.get(options1));
                categoryId = categories.get(options1).getId();
            }
        })
                .setSelectOptions(0)//设置选择第一个
                .setOutSideCancelable(false)//点击背的地方不消失
                .build();//创建
               pvOptions.setPicker(listData);

        pvOptions.show();
    }

    /**
     * 造一组数据
     */
    private List<String> getData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < categories.size(); i++) {
            list.add(categories.get(i).getName());
        }
        return list;

    }


    @Override
    public void setSucc(String msg) {
        TToast("提交成功!");
        finish();
    }

    @Override
    public void setFial(String msg) {
        TToast(msg);
    }

    @Override
    public void setCategory(List<Category> categorys) {
        categorys.remove(0);
        categories = new ArrayList<>();
        categories.addAll(categorys);
        categoryId = categorys.get(0).getId();
        type_tv.setText(categorys.get(0).getName());
    }


    @Override
    protected void afterCreate() {

    }


    @Override
    protected CheckInPresenter createPresenter() {
        return mPresenter = new CheckInPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_in;
    }


}
